package com.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.dto.UserProfileDTO;

public class UserProfileService 
{
	public void add(UserProfileDTO dto)
	{
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(dto);
		transaction.commit();
		session.close();
		factory.close();
	}
	
	public UserProfileDTO get(UserProfileDTO dto)
	{
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		dto = (UserProfileDTO)session.get(UserProfileDTO.class, dto.getPanNo());
		return dto;
	}
	
	public void update(UserProfileDTO dto)
	{
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction transaction = session.beginTransaction();
		session.update(dto);
		transaction.commit();
		session.close();
		factory.close();
	}
	
	public void delete(UserProfileDTO dto)
	{
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(dto);
		transaction.commit();
		session.close();
		factory.close();
	}
	
	public List getList()
	{
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Query query = session.createQuery("from UserProfileDTO");
		List list = query.list();
		return list;
	}

}
